(function() {
  var validate_acct_info;

  validate_acct_info = function(form) {
    var acct_name, acct_no, cred_no, cred_type, result;
    cred_type = form.form('get value', 'acct_cred_type');
    cred_no = form.form('get value', 'acct_cred_no');
    if (parseInt(cred_type) === 1 && cred_no.trim().length > 0) {
      result = validate_idno(cred_no, '收款账户开户人');
      if (!result.succ) {
        alert(result.errmsg);
        return false;
      }
    }
    acct_name = form.form('get value', 'acct_name');
    if (cred_no.trim().length === 0 && acct_name.trim().length === 0) {
      alert('账户开户人姓名和证件号码必须至少填写一个');
      return false;
    }
    acct_no = form.form('get value', 'acct_no');
    if (acct_no.length > 0) {
      if (!validate_cardno(acct_no)) {
        return confirm("收款账号可能存在错误，选择［取消］按钮返回检查，或［确定］按钮继续提交.");
      }
    }
    return true;
  };

  $(function() {
    var gather_info;
    gather_info = {};
    $('form#form1').form({
      inline: true,
      fields: {
        merch_type: {
          optional: false,
          rules: [
            {
              type: 'empty'
            }
          ]
        }
      }
    });
    $('form#form1_1').form({
      inline: true,
      fields: {
        t20_merno: {
          optional: false,
          rules: [
            {
              type: 'empty'
            }, {
              type: 'doesntContain[ ]'
            }
          ]
        },
        t20_termid: {
          optional: false,
          rules: [
            {
              type: 'empty'
            }, {
              type: 'doesntContain[ ]'
            }
          ]
        },
        name: {
          optional: false,
          rules: [
            {
              type: 'empty'
            }
          ]
        },
        contact: {
          optional: false,
          rules: [
            {
              type: 'empty'
            }
          ]
        },
        mobile: {
          optional: true,
          rules: [
            {
              type: 'integer[..]'
            }
          ]
        },
        address: {
          optional: false,
          rules: [
            {
              type: 'empty'
            }
          ]
        },
        email: {
          optional: true,
          rules: [
            {
              type: 'email'
            }
          ]
        },
        tz0_merno: {
          optional: false,
          rules: [
            {
              type: 'minLength[4]'
            }, {
              type: 'doesntContain[ ]'
            }
          ]
        },
        tz0_termid: {
          optional: false,
          rules: [
            {
              type: 'empty'
            }, {
              type: 'doesntContain[ ]'
            }
          ]
        },
        tz0_macalgo: {
          optional: false,
          rules: [
            {
              type: 'empty'
            }
          ]
        },
        tz0_tmk: {
          optional: false,
          rules: [
            {
              type: 'minLength[32]'
            }, {
              type: 'maxLength[32]'
            }, {
              type: 'regExp[/^[0-9a-fA-F]+$/]'
            }
          ]
        }
      }
    });
    $('form#form1_2').form({
      inline: true,
      fields: {
        t20_fee: {
          optional: false,
          rules: [
            {
              type: 'empty'
            }
          ]
        },
        acct_no: {
          optional: false,
          rules: [
            {
              type: 'empty'
            }, {
              type: 'integer[..]'
            }
          ]
        },
        acct_name: {
          optional: true,
          rules: [
            {
              type: 'doesntContain[ ]'
            }
          ]
        },
        acct_cred_type: {
          optional: true,
          rules: [
            {
              type: 'integer[..]'
            }
          ]
        },
        acct_mobile: {
          optional: true,
          rules: [
            {
              type: 'integer[..]'
            }
          ]
        },
        pay_chnl: {
          optional: false,
          rules: [
            {
              type: 'empty'
            }
          ]
        },
        pay_merid: {
          optional: false,
          rules: [
            {
              type: 'empty'
            }
          ]
        }
      }
    });
    $('form#form1_3').form({
      inline: true,
      fields: {}
    });
    $('form#form1_4').form({
      inline: true,
      fields: {}
    });
    $('form#form2_1').form({
      inline: true,
      fields: {
        t60_merno: {
          optional: false,
          rules: [
            {
              type: 'empty'
            }, {
              type: 'doesntContain[ ]'
            }
          ]
        },
        t60_termid: {
          optional: false,
          rules: [
            {
              type: 'empty'
            }, {
              type: 'doesntContain[ ]'
            }
          ]
        },
        name: {
          optional: false,
          rules: [
            {
              type: 'empty'
            }
          ]
        },
        contact: {
          optional: false,
          rules: [
            {
              type: 'empty'
            }
          ]
        },
        mobile: {
          optional: true,
          rules: [
            {
              type: 'integer[..]'
            }
          ]
        },
        address: {
          optional: false,
          rules: [
            {
              type: 'empty'
            }
          ]
        },
        email: {
          optional: true,
          rules: [
            {
              type: 'email'
            }
          ]
        },
        tz1_merno: {
          optional: false,
          rules: [
            {
              type: 'minLength[4]'
            }, {
              type: 'doesntContain[ ]'
            }
          ]
        },
        tz1_termid: {
          optional: false,
          rules: [
            {
              type: 'empty'
            }, {
              type: 'doesntContain[ ]'
            }
          ]
        },
        tz1_macalgo: {
          optional: false,
          rules: [
            {
              type: 'empty'
            }
          ]
        },
        tz1_tmk: {
          optional: false,
          rules: [
            {
              type: 'minLength[32]'
            }, {
              type: 'maxLength[32]'
            }, {
              type: 'regExp[/^[0-9a-fA-F]+$/]'
            }
          ]
        }
      }
    });
    $('form#form2_2').form({
      inline: true,
      fields: {
        t60_fee: {
          optional: false,
          rules: [
            {
              type: 'empty'
            }
          ]
        },
        p2_fund: {
          optional: false,
          rules: [
            {
              type: 'empty'
            }
          ]
        },
        fee_take_type: {
          optional: false,
          rules: [
            {
              type: 'empty'
            }
          ]
        },
        fund_lack_proc: {
          optional: false,
          rules: [
            {
              type: 'empty'
            }
          ]
        },
        acct_no: {
          optional: false,
          rules: [
            {
              type: 'empty'
            }, {
              type: 'integer[..]'
            }
          ]
        },
        acct_name: {
          optional: true,
          rules: [
            {
              type: 'doesntContain[ ]'
            }
          ]
        },
        acct_cred_type: {
          optional: true,
          rules: [
            {
              type: 'integer[..]'
            }
          ]
        },
        acct_mobile: {
          optional: true,
          rules: [
            {
              type: 'integer[..]'
            }
          ]
        },
        pay_chnl: {
          optional: false,
          rules: [
            {
              type: 'empty'
            }
          ]
        },
        pay_merid: {
          optional: false,
          rules: [
            {
              type: 'empty'
            }
          ]
        }
      }
    });
    $('form#form2_3').form({
      inline: true,
      fields: {}
    });
    $('form#form2_4').form({
      inline: true,
      fields: {}
    });
    $('.modal.step1 .dropdown.merch_type').dropdown({
      onChange: function(value, text, $choice) {
        return $('.modal.step1 .button.next').data('next', "step" + value + "_1");
      }
    });
    $('.ui.modal.step1').modal({
      closable: false
    }).modal('show');
    $('button.startup').click(function() {
      $('.ui.modal').modal('hide all');
      return $('.ui.modal.step1').modal('show');
    });
    $('.button.next').click(function() {
      var form, k, next, v, values;
      form = $(this).parents('.ui.modal').find('form.form');
      if (!form.form('is valid')) {
        return;
      }
      if (form.data('acct-info')) {
        if (!validate_acct_info(form)) {
          return;
        }
      }
      values = form.form('get values');
      for (k in values) {
        v = values[k];
        gather_info[k] = v;
      }
      next = $(this).data('next');
      if ($(".ui.modal." + next).length === 0) {
        return alert('下一步窗口不存在，请检查...');
      }
      return $(".ui.modal." + next).modal('hide others').modal({
        closable: false
      }).modal('show');
    });
    $('.button.prev').click(function() {
      var prev;
      prev = $(this).data('prev');
      return $(".ui.modal." + prev).modal('hide others').modal({
        closable: false
      }).modal('show');
    });
    $('.button.finish').click(function() {
      var $this, action, form, k, v, values;
      form = $(this).parents('.ui.modal').find('form.form');
      if (!form.form('is valid')) {
        return;
      }
      values = form.form('get values');
      for (k in values) {
        v = values[k];
        gather_info[k] = v;
      }
      action = $(this).data('action');
      $this = $(this).addClass('loading');
      return $.post(action, {
        info: gather_info
      }, function(resp) {
        $this.removeClass('loading');
        if (!resp.succ) {
          return;
        }
        $this.parents('.modal').modal('hide');
        gather_info = {};
        return alert('新商户入户成功...');
      });
    });
    $('a#tmk_gen').click(function(evt) {
      var i, key, t, _i;
      evt.preventDefault();
      key = '';
      for (i = _i = 0; _i <= 31; i = ++_i) {
        t = Math.ceil(Math.random() * 15);
        t = t > 9 ? t - 10 + 65 : t + 48;
        key += String.fromCharCode(t);
      }
      return $(this).siblings('input.tmk').val(key);
    });
    $('.dropdown.pay_chnl').dropdown({
      onChange: function(value, text, $choice) {
        $(this).addClass('loading');
        return $.post('/wizard/ajax/pay_merids', {
          chnl: value
        }, function(resp) {
          var items, merid, _i, _len, _ref;
          $('.dropdown.pay_chnl').removeClass('loading');
          if (!resp.succ) {
            return;
          }
          $('.dropdown.pay_merid').dropdown('clear');
          items = '';
          _ref = resp.merids;
          for (_i = 0, _len = _ref.length; _i < _len; _i++) {
            merid = _ref[_i];
            items += "<div class='item' data-value='" + merid.uuid + "'>";
            items += "" + merid.merno;
            items += "<div class='right floated' style='font-size:small; color:#888;'>" + merid.name;
            items += "</div>";
            items += "</div>";
          }
          $('.dropdown.pay_merid .menu').html(items);
          return $('.dropdown.pay_merid').dropdown('refresh');
        });
      }
    });
    $('.ui.button#trcode_all').click(function() {
      return $(this).parents('form').find('.checkbox.trcode').each(function() {
        return $(this).checkbox('check');
      });
    });
    $('.ui.button#trcode_none').click(function() {
      return $(this).parents('form').find('.checkbox.trcode').each(function() {
        return $(this).checkbox('uncheck');
      });
    });
    return $('.dropdown.trctl_t').dropdown({
      onChange: function(value, text, $choice) {
        var $form, $this;
        $form = $(this).parents('form');
        $this = $(this).addClass('loading');
        return $.post('/wizard/ajax/trctl', {
          trctl_uuid: value,
          trcode: $(this).data('trcode')
        }, function(resp) {
          $this.removeClass('loading');
          if (!resp.succ) {
            return;
          }
          return $form.form('set values', resp.trctl);
        });
      }
    });
  });

}).call(this);
